package co.edu.unal.geditor.controller.action.undoable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.swing.undo.AbstractUndoableEdit;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;

import co.edu.unal.geditor.controller.App;
import co.edu.unal.geditor.model.Drawing;
import co.edu.unal.geditor.model.GraphElement;

public class UndoableDelete extends AbstractUndoableEdit {
	private static final long serialVersionUID = 1L;

	public UndoableDelete(Map<GraphElement,String> ids,
			ArrayList<GraphElement> deletedElements) {
		idx = ids;
		this.deletedElements = deletedElements;
	}

	public void redo() throws CannotRedoException {
		Drawing drawing = App.getInstance().getDrawing();
		for (int i=deletedElements.size()-1; i>=0;i--) {
			GraphElement ge=deletedElements.get(i);
			drawing.removeElementAt(Integer.valueOf(idx.get(ge)));
		}

	}

	public void undo() throws CannotUndoException {
		Drawing drawing = App.getInstance().getDrawing();
		for (int i=deletedElements.size()-1; i>=0;i--) {
			GraphElement ge=deletedElements.get(i);
			drawing.insertElementAt(ge, Integer.valueOf(idx.get(ge)));
		}
	}

	public boolean canUndo() {
		return (deletedElements != null);
	}

	public boolean canRedo() {
		return (deletedElements != null);
	}

	public String getPresentationName() {
		return "Delete Element";
	}

	Map<GraphElement,String> idx = new HashMap<GraphElement,String>();
	ArrayList<GraphElement> deletedElements = new ArrayList<GraphElement>();
}
